package com.gorkr.hot100.medium;

import java.util.Arrays;

import org.junit.Test;

/**
 * @author 张心宇 <zhangxinyu20@kuaishou.com>
 * Created on 2022-12-21
 */
public class M75SortColor {

    public void sortColors(int[] nums) {
        // 冒泡排序
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                }
            }
        }
    }

    public void sortColors2(int[] nums) {
        // 单指针
        int ptr = 0;
        for (int i = ptr; i < nums.length; i++) {
            if(nums[i]==0) {
                int tmp = nums[ptr];
                nums[ptr] = nums[i];
                nums[i] = tmp;
                ptr++;
            }
        }
        for (int i = ptr; i < nums.length; i++) {
            if(nums[i]==1) {
                int tmp = nums[ptr];
                nums[ptr] = nums[i];
                nums[i] = tmp;
                ptr++;
            }
        }
    }

    @Test
    public void test() {
        int[] array = {2, 0, 2, 1, 1, 0};
        sortColors2(array);
        System.out.println(Arrays.toString(array));

    }
}
